package structures;

import client.CtfState;
import gui.*;

/**
 * Position (index for maze)
 * 		row, column
 * 		min value: 1, 1
 *		max value: Maze.numRows, Maze.numCols
 */
public class Position implements Comparable<Position> {
	/*
	 * Instance Variables
	 */
	public int row;
	public int col;
	int rowIndex;
	int colIndex;
	
	/*
	 * Constructor
	 */
	public Position( int r, int c ) {
		row = r;
		col = c;
		rowIndex = r-1;
		colIndex = c-1;
	}
	
	/* Accessors */
	public int getRow() { return row; }
	public int getCol() { return col; }
	public int getRowIndex() { return rowIndex; }
	public int getColIndex() { return colIndex; }
	// index into cells array
	public int getIndex(int numCols) {
		return row*numCols + col-1 ;
	}
	public int getMyGraphicalPosX() {
		return getGraphicalPosX( this );
	}
	public int getMyGraphicalPosY() {
		return getGraphicalPosY( this );
	}

	
	public boolean isInvalid() {
		if ( row == 0 || col == 0 )
			return true;
		return false;
	}
	
	/*
	 * Movement methods
	 * 		return new Positions
	 */
	public Position up() {
		return new Position( row-1, col );
	}	
	public Position down() {
		return new Position( row+1, col );
	}
	public Position left() {
		return new Position( row, col-1 );
	}
	public Position right() {
		return new Position( row, col+1 );
	}

	
	/*
	 * Static methods to get graphical positions
	 * 		center around myPlayer
	 */
	public static int getGraphicalPosX ( Position p ) {
		return (p.colIndex * CtfGui.CELL_LENGTH);
	}
	public static int getGraphicalPosY ( Position p ) {
		return (p.rowIndex * CtfGui.CELL_LENGTH);
	}
	public static int getCenteredGraphicalPosX ( Position p ) {
		return (p.colIndex * CtfGui.CELL_LENGTH)
		- CtfState.getMyPlayer().getPos().getMyGraphicalPosX() 
	    				+ ( CtfGui.WINDOW_WIDTH/2 );
	}
	public static int getCenteredGraphicalPosY ( Position p ) {
		return (p.rowIndex * CtfGui.CELL_LENGTH)
		- CtfState.getMyPlayer().getPos().getMyGraphicalPosY()
	    				+ ( CtfGui.WINDOW_HEIGHT/2 );
	}
	public static int getGraphicalPosX ( int r, int c ) {
		return getGraphicalPosX( new Position(r,c) );
	}
	public static int getGraphicalPosY ( int r, int c ) {
		return getGraphicalPosY( new Position(r,c) );
	}

	
	/**
	 * Implements Comparable for equality testing.
	 * 
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo ( Position other ) {
		if ( this.row <= other.row && this.col < other.col ) 
			return -1;
		else if ( this.row == other.row && this.col == other.col )
			return 0;
		else
			return 1;
	}
	
	public String toString() {
		return "(" + this.row + "," + this.col + ")";
	}
}
